<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="填报模板" prop="temepleteId">
        <el-select v-model="queryParams.templeteId" placeholder="请选择" style="width: 240px" clearable>
          <el-option
            v-for="item in templeteOptions"
            :key="item.templeteId"
            :label="item.templeteName"
            :value="item.templeteId" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="填报状态" prop="fillStatus">
        <el-select v-model="queryParams.fillStatus" placeholder="请选择填报状态" style="width: 240px" clearable>
          <el-option
            v-for="dict in dict.type.fill_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="填报时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['report:filldata:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['report:filldata:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filldataList" @selection-change="handleSelectionChange" height="330px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="填报数据id" align="center" prop="filldataId" />
      <el-table-column label="填报模板名称" align="center" prop="templeteName" />
      <el-table-column label="填报状态" align="center" prop="fillStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fill_status" :value="scope.row.fillStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="填报时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.fillStatus==='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['report:filldata:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.fillStatus==='1'"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetail(scope.row)"
            v-hasPermi="['report:filldata:remove']"
          >查看详情</el-button>
          <el-button 
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['report:filldata:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.current"
      :limit.sync="pageParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改填报数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" :fullscreen="true" :before-close="saveTip" append-to-body>
        <iframe id="sheetFrame" :src="iframeUrl" frameborder="0" width="100%" :height="pageHeight" style="margin-top: -25px;"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {listFilldata, delFilldata} from "@/api/report/filldata";
import {listCanfillTempletedesign} from "@/api/report/templetedesign";

export default {
  name: "Filldata",
  dicts: ['fill_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 填报数据表格数据
      filldataList: [],
      //可填报模板列表
      templeteOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 填报状态时间范围
      daterangeCreateTime: [],
      // 分页参数
      pageParams: {
        current: 1,
        size: 10,
      },
      // 查询参数
      queryParams: {
        dataId: null,
        templeteId: null,
        templeteName: null,
        fillStatus: null,
        createTime: null,
        beginCreateTime: null,
        endCreateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      pageHeight: 580,
      iframeUrl: '/report/fill_templete.html'
    };
  },
  created() {
    this.pageHeight = document.documentElement.clientHeight - 90;
    window.localStorage.setItem("currenv",JSON.stringify(process.env));
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.getCanfillTempleteList();
      vm.handleQuery();
    })
  },
  mounted() {
    window.closeCommitData = this.closeCommitData;
  },
  methods: {
    /** 查询流程分类列表 */
    getCanfillTempleteList() {
      listCanfillTempletedesign().then(response => {
        this.templeteOptions = response.data;
        window.localStorage.setItem("canFillTempleteList",JSON.stringify(this.templeteOptions));
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.open = false;
      this.pageParams.current = 1;
      this.getList();
    },
    /** 查询填报数据列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams["endCreateTime"] = this.daterangeCreateTime[1];
      } else {
        this.queryParams["beginCreateTime"] = null;
        this.queryParams["endCreateTime"] = null;
      }
      listFilldata(this.pageParams,this.queryParams).then(response => {
        this.filldataList = response.page.records;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dataId: null,
        templeteId: null,
        templeteName: null,
        userId: null,
        userName: null,
        fillContent: null,
        fillStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.filldataId);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增填报";
      this.iframeUrl = '/report/fill_templete.html';
      window.localStorage.removeItem("updateTempleteInfo");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改填报";
      this.iframeUrl = '/report/fill_templete.html';
      window.localStorage.setItem("updateTempleteInfo",JSON.stringify(row));
    },
    /** 查看详情操作 */
    viewDetail(row) {
      this.reset();
      this.open = true;
      this.title = "填报详情";
      this.iframeUrl = '/report/view_templete.html';
      window.localStorage.setItem("updateTempleteInfo",JSON.stringify(row));
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var dataIds = this.ids;
      if(row.filldataId){
        dataIds = new Array();
        dataIds.push(row.filldataId);
      }
      this.$modal.confirm('是否确认删除选定的数据项？').then(function() {
        return delFilldata(dataIds);
      }).then((res) => {
      	if(res.code==='200') {
          this.handleQuery();
          this.$modal.msgSuccess(res.msg);
      	}else {
          this.$modal.msgError(res.msg);
      	}
      }); 
    },
    /** 保存提示 */
    saveTip() {
      this.handleQuery();
    },
    closeCommitData(queryFillTemplete) {
      if(queryFillTemplete){
        this.open = false;
        this.getCanfillTempleteList();
      }else{
        this.handleQuery();
      }
    }
  }
};
</script>
